package com.jy.wms.controller;

import com.joyi.gate.comm.SessionInfo;
import com.joyi.gate.safety.vo.UserInfo;
import com.jy.wms.common.SetStatus;
import com.jy.wms.common.base.*;
import com.jy.wms.pojo.*;
import io.swagger.annotations.ApiParam;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import com.jy.wms.service.WmsLineService;
import javax.servlet.http.HttpSession;
import com.github.pagehelper.Page;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.jy.wms.common.UUIDUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

/**
 * @ClassName: WmsLineController
 * @Description: TODO(这里用一句话描述这个类的作用)
 * @Author ln
 * @Date Tue Oct 17 14:42:38 CST 2017
 */
@RestController
@RequestMapping("/wmsline")
@Api(value = "/wmsline", description = "线路")
public class  WmsLineController extends BaseController<WmsLineService,WmsLine> {

    @PostMapping(value = "/list")
    @ApiOperation(value = "线路查询信息",response = JSONObject.class, notes = "线路查询信息")
    public @ResponseBody
        JSONObject  getWmsLine(@RequestBody WmsLine entity,HttpSession session) {
        Page<WmsLine> list =new Page<WmsLine>();
        JSONObject json = getJSONObject();
        try{
            // 获取user
            UserInfo userinfo= (UserInfo) session.getAttribute(SessionInfo.USER_INFO);
            //获取仓库id
            entity.setCkid(String.valueOf(userinfo.getUser().getOrgId().get(0)));
            //获取货主id
            List<SysHz> hz= (List<SysHz>) session.getAttribute(BaseSessionInfo.SessionConsignor);
            entity.setHzid(hz.get(0).getHzid());
            list= baseService.baseSelectPage(entity);
            json = SetStatus.setSearchType(JSONArray.fromObject(list).toString()
                    ,(int) list.getTotal());
        }catch (Exception e){
            e.printStackTrace();
        }
        return json;
    }

    @ApiOperation(value = "添加线路",response = JSONObject.class, notes = "POST新增线路")
    @PostMapping(value = "/add")
    public @ResponseBody
    JSONObject addWmsrRailwayPlatform(@RequestBody WmsLine entity, HttpSession session) throws Exception {
       JSONObject json=new JSONObject();
        try {
            // 获取user
            UserInfo userinfo = (UserInfo) session.getAttribute(SessionInfo.USER_INFO);
            //获取仓库id
            entity.setCkid(String.valueOf(userinfo.getUser().getOrgId().get(0)));
            //获取货主id
            List<SysHz> hz = (List<SysHz>) session.getAttribute(BaseSessionInfo.SessionConsignor);
            entity.setHzid(hz.get(0).getHzid());
            baseService.baseInsert(entity);
        }catch (SucessException e){
            e.printStackTrace();
            json= SetStatus.setFunctionType(ReturnType.messageType_top.getType(),ReturnType.success.toString(),"新增线路成功",e.getMessage(), JSONObject.fromObject(entity).toString());
        }catch (ServiceException e){
            e.printStackTrace();
            json= SetStatus.setFunctionType(ReturnType.messageType_right.getType(),ReturnType.warning.toString(),"新增线路失败",e.getMessage(),"");
        }catch (Exception e){
            e.printStackTrace();
            json= SetStatus.setFunctionType(ReturnType.messageType_right.getType(),ReturnType.error.toString(),"新增线路失败",e.getMessage(),"");
        }
        return json;
    }

    @GetMapping(value = "/baseSelectByPrimaryKey/{id}")
    @ApiOperation(value = "根据id查询路线",response = JSONObject.class, notes = "GET查看单个路线")
    public  @ResponseBody JSONObject baseSelectByPrimaryKey(@PathVariable("id") String id) {
        JSONObject json = new JSONObject();
        WmsLine entity= null;
        try {
            entity = baseService.baseSelectByPrimaryKey(id);
            json = JSONObject.fromObject(entity);
        } catch (Exception e) {
            e.printStackTrace();
            json = SetStatus.setFunctionType(ReturnType.messageType_right.getType(),ReturnType.error.toString(),"查询路线失败","查询路线信息失败","");
        }
        return json;
    }

    @ApiOperation(value = "编辑线路",response = JSONObject.class, notes = "POST新增线路")
    @PostMapping(value = "/baseUpdateByPrimaryKeySelective")
    public  @ResponseBody JSONObject baseUpdateByPrimaryKeySelective(@RequestBody WmsLine entity,HttpSession session) throws Exception {
        JSONObject json = new JSONObject();
        try {
            // 获取user
            UserInfo userinfo = (UserInfo) session.getAttribute(SessionInfo.USER_INFO);
            //获取仓库id
            entity.setCkid(String.valueOf(userinfo.getUser().getOrgId().get(0)));
            //获取货主id
            List<SysHz> hz = (List<SysHz>) session.getAttribute(BaseSessionInfo.SessionConsignor);
            entity.setHzid(hz.get(0).getHzid());
            baseService.baseUpdateByPrimaryKeySelective(entity);
        }catch (SucessException e){
            e.printStackTrace();
            json= SetStatus.setFunctionType(ReturnType.messageType_top.getType(),ReturnType.success.toString(),"编辑线路成功",e.getMessage(), JSONObject.fromObject(entity).toString());
        }catch (ServiceException e){
            e.printStackTrace();
            json= SetStatus.setFunctionType(ReturnType.messageType_right.getType(),ReturnType.warning.toString(),"编辑线路失败",e.getMessage(),"");
        }catch (Exception e){
            json= SetStatus.setFunctionType(ReturnType.messageType_right.getType(),ReturnType.error.toString(),"编辑线路失败",e.getMessage(),"");
        }
        return json;
    }

    @ApiOperation(value = "线路作废 需要传id ",response = JSONObject.class, notes = "POST线路作废")
    @PostMapping(value = "zuoFie")
    public @ResponseBody
    JSONObject baseZuoFei(@ApiParam(required = true, name = "setModel", value = "配置参数映射对象") @RequestBody SetAndModifyModel setModel) throws Exception {
        JSONObject json = new JSONObject();
        try {
            String sfzf=PlatformInfo.linef_sfzf.getType();
            baseService.baseZuoFei(setModel.getIds(),sfzf);
        }catch (SucessException e){
            e.printStackTrace();
       json=SetStatus.setFunctionType(ReturnType.messageType_top.getType(),ReturnType.success.toString(),"路线作废成功","路线作废","");
    }catch (ServiceException e){
            json= SetStatus.setFunctionType(ReturnType.messageType_right.getType(),ReturnType.warning.toString(),"路线作废失败",e.getMessage(),"");
    }catch (Exception e){
            json= SetStatus.setFunctionType(ReturnType.messageType_right.getType(),ReturnType.error.toString(),"路线作废失败",e.getMessage(),"");
        }
        return json;
}

    @ApiOperation(value = "线路激活 需要传id ",response = JSONObject.class, notes = "POST线路激活")
    @PostMapping(value = "JiHuo")
    public @ResponseBody
    JSONObject baseJiHuo(@ApiParam(required = true, name = "setModel", value = "配置参数映射对象") @RequestBody SetAndModifyModel setModel) throws Exception {
        JSONObject json = new JSONObject();
        try {
            String sfzf=PlatformInfo.linej_sfzf.getType();
            baseService.baseJiHuo(setModel.getIds(),sfzf);
        }catch (SucessException e){
            e.printStackTrace();
            json=SetStatus.setFunctionType(ReturnType.messageType_top.getType(),ReturnType.success.toString(),"路线激活成功","路线作废","");
        }catch (ServiceException e){
            json= SetStatus.setFunctionType(ReturnType.messageType_right.getType(),ReturnType.warning.toString(),"路线激活失败",e.getMessage(),"");
        }catch (Exception e){
            json= SetStatus.setFunctionType(ReturnType.messageType_right.getType(),ReturnType.error.toString(),"路线激活失败",e.getMessage(),"");
        }
        return json;
    }

    @ApiOperation(value = "集货库位查询",response = JSONObject.class, notes = "get集货库位查询")
    @GetMapping(value = "/hzSearch")
    public  @ResponseBody JSONArray jhKwz(HttpSession session) {
        JSONArray jsonArray = new JSONArray();
        JSONObject json=null;
        JSONArray jsonArray2=null;
        JSONObject json2=null;
        List<WmsLibrarypositiongroup>list=new ArrayList<WmsLibrarypositiongroup>();
        try {
            //获取货主id
            List<SysHz> hz= (List<SysHz>) session.getAttribute(BaseSessionInfo.SessionConsignor);
            if (hz.size()>0){
                String sshzid=hz.get(0).getHzid();
                String kwzlx=PlatformInfo.baseGroup_kwzlx.getType();
                String sfyx = PlatformInfo.baseGroup_sfyx.getType();
                list=baseService.jhKwz(sshzid,kwzlx,sfyx);
                for (int i = 0; i < list.size(); i++) {
                     json = new JSONObject();
                    json.put("value",list.get(i).getId());
                    json.put("label",list.get(i).getKwzmc());
                   jsonArray2 = new JSONArray();
                  for(int j = 0; j < list.get(i).getList().size(); j++){
                        json2 = new JSONObject();
                        json2.put("value",list.get(i).getList().get(j).getId());
                        json2.put("label",list.get(i).getList().get(j).getKwmc());
                        jsonArray2.add(json2);
                    }
                    json.put("children",jsonArray2);
                    jsonArray.add(json);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
          }
        return jsonArray;
    }

    /**
     * 库位查询
     * @param map
     * @return
     */
    /*@PostMapping(value = "/hzSearch",produces = {"application/json;charset=UTF-8"})
    @ApiOperation(value = "库位查询",response = JSONObject.class, notes = "查询输入{\"sskwzid\":\"库位组id\",\"mc\":\"库位模糊查询\"}")
    public @ResponseBody
    JSONArray hzSearch(@RequestBody Map<String,String> map) {
        JSONArray jsonArray = new JSONArray();
        try {
            List<WmsLibraryPosition> list = baseService.hzSearch(map.get("sskwzid"),map.get("mc"));
            for (int i = 0; i < list.size(); i++) {
                JSONObject json = new JSONObject();
                json.put("dictionaryid",list.get(i).getId());
                json.put("value",list.get(i).getKwmc());
                jsonArray.add(json);
            }
        } catch (Exception e) {
            logger.error(e.getMessage());
            e.printStackTrace();
        }
        return jsonArray;
    }*/

    /**
     * 根据线路id 查询线路明细
     * @param id
     * @return
     */
  @ApiOperation(value = "查询路线省市",response = JSONObject.class, notes = "POST查询路线省市)")
    @GetMapping(value = "/lingMxSelect/{id}")
    public @ResponseBody
    JSONObject lingMxSelect(@PathVariable("id") String id) {
        List<WmsLineDetailed> list=new ArrayList<WmsLineDetailed>();
        JSONObject json = getJSONObject();
        try {
            list=baseService.lingMxSelect(id);
            json = SetStatus.setSearchType(JSONArray.fromObject(list).toString()
                    ,list.size());
        }catch (ServiceException e){
            json = SetStatus.setFunctionType(1,"error","路线省市查询失败",e.getMessage(),"");

        }catch (Exception e){
            e.printStackTrace();
            json = SetStatus.setFunctionType(1,"error","路线省市查询失败",e.getMessage(),"");
        }
        return json;
    }
}
